<template>
	<view class="content">
		<navBar>
			<template @click="backPage()" slot="left">
				<u-icon name="arrow-left" color="#fff"></u-icon>
			</template>
			<template class="centerText" slot="center">
				<view class="" style="color: #fff">货物管理</view>
			</template>
		</navBar>

		<view class="popup_content">
			<view class="top_box">
				<view class="btn" @click="add()">
					新增
				</view>
				<view class="ipt_box">
					<!-- <u-icon name="scan" color="#2979ff" size="28"></u-icon> -->
					<input v-model="pageInfo.goods_name" @input="searchGoods()" placeholder="输入关键字查找货物" type="text" />
				</view>
				<!-- <view @click="close()" class="text_box">
					选好了
				</view> -->
			</view>

			<view class="goods_box">
				<view class="left_box">
					<view @click="chooseSort(-1,0)" :class="{sort_item_active:activeIndex == -1}" class="sort_item ">
						全部
					</view>
					<view @click="chooseSort(index,item.cate_id)" :class="{sort_item_active:activeIndex == index}"
						v-for="item,index in sortList" :key="item.cate_id" class="sort_item ">
						{{item.cate_name}}
					</view>
				</view>

				<view class="right_box">
					<view class="empty_box" v-if="goodsList.length == 0">
						<u-empty text="暂无货物"></u-empty>
					</view>
					<view v-else>
						<view v-for="item,index in goodsList" :key='item.goods_id' class="goods_item">
							<view style="display: flex;">
								<view class="img_box">
									<image @error="imageError($event,index)"
										:src="item.goods_img && item.goods_img!='' ? item.goods_img : defaultImg"
										mode="">
									</image>
								</view>
								<view class="info">
									<view class="goodsName hidden_text">
										{{item.goods_name}}
									</view>
									<view class="goodsNum">
										库存：{{item.goods_num}}<text v-if="item.unit != ''">({{item.unit}})</text>
									</view>
								</view>
							</view>
							<view class="addGoods">
								<view style="margin-right: 20rpx;" @click.stop="deleteGoods(item.goods_id)"
									class="right">
									删除
								</view>
								<view @click.stop="add(item)" class="right">
									编辑
								</view>
							</view>
						</view>
						<pagination :status="status" @refreshList='getGoodsById()' @changeStatus='handelChangeStatus()'>
						</pagination>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import navBar from '@/components/navBar.vue'
	import pagination from '@/components/pagination.vue'
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	import {
		getTotalCategoryListApi,
		getGoodsByIdApi,
		deleteGoodsApi
	} from '@/api/categoryManage.js'
	export default {
		data() {
			return {
				timer: '',
				sortList: [],
				activeIndex: -1,
				goodsList: [],
				value: 0,
				defaultImg: '/static/images/shixiao.png',
				pageInfo: {
					page: 1,
					pageSize: 30,
					goods_name: "",
					cate_id: 0,
					ware_id: this.$store.state.user.stashInfo.ware_id
				},
				status: 'loadmore',
				count: ''
			}
		},
		props: {
			show: {
				type: Boolean,
				default: false
			},
			is_back: {
				default: 0
			}
		},
		computed: {
			...mapGetters([
				'cartList',
			])
		},
		// mounted() {
		// 	this.getTotalCategoryList()
		// 	this.getGoodsById()
		// },
		onShow() {
			this.getTotalCategoryList()
			this.pageInfo.page = 1
			this.goodsList = []
			this.getGoodsById()
		},
		components: {
			navBar,
			pagination
		},

		methods: {
			backPage() {
				uni.navigateBack();
				this.$store.state.refresh = Number(new Date())
			},
			add(item) {
				if (item) {
					delete item.create_by
					delete item.create_time
					delete item.org_id
					delete item.update_by
					delete item.update_time
					const res = JSON.stringify(item)
					uni.navigateTo({
						url: `/commonToolPages/cargoManage/cargoManage?goodsInfo=${res}`
					})
				} else {
					uni.navigateTo({
						url: '/commonToolPages/cargoManage/cargoManage'
					})
				}
			},
			//搜索商品
			searchGoods() {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.goodsList = []
					this.pageInfo.page = 1
					this.getGoodsById()
				}, 500)
			},
			//获取分类列表
			async getTotalCategoryList() {
				const {
					data: {
						data
					}
				} = await getTotalCategoryListApi()
				this.sortList = data
			},
			chooseSort(index, cate_id) {
				this.goodsList = []
				this.pageInfo.page = 1
				this.activeIndex = index
				this.pageInfo.cate_id = cate_id
				this.getGoodsById()
			},
			//根据分类Id获取商品
			async getGoodsById() {
				const {
					data: {
						data: {
							list,
							count
						}
					}
				} = await getGoodsByIdApi({
					...this.pageInfo,
					is_back: this.is_back
				})
				this.count = count
				this.goodsList = [...this.goodsList, ...list]
				if (this.goodsList.length == this.count) {
					this.status = 'nomore'
				} else {
					this.status = 'loadmore'
				}
				this.pageInfo.page++
				// // 使用 Map 存储 cartList 中的商品 ID 和数量
				// const cartItemQuantityMap = new Map(this.cartList.map(item => [item.goods_id, item.quantity]));
				// // 使用map比较并更新 goodsList 中的数量
				// this.goodsList = list.map(item => ({
				// 	...item,
				// 	quantity: cartItemQuantityMap.has(item.goods_id) ? cartItemQuantityMap.get(item
				// 		.goods_id) : 0
				// }));
			},
			...mapMutations('cart', ['addToCart', 'upToCart', 'clearCart', 'SELF_INPUT']),
			// 添加
			addGoods(item) {
				this.addToCart(item)
			},
			//减少
			decreaseGoods(item) {
				if (item.quantity <= 0) return
				this.upToCart(item)
			},
			//输入框添加
			selfInput(item) {
				this.SELF_INPUT(item)
			},
			//图片发生错误换成默认图片
			imageError(e, imgIndex) {
				if (this.goodsList.length > 0) {
					this.goodsList[imgIndex].goods_img = this.defaultImg
				}
			},
			//删除商品
			async deleteGoods(goods_id) {
				const that = this
				uni.showModal({
					content: '确定要删除此商品吗',
					success: async function(res) {
						if (res.confirm) {
							await deleteGoodsApi({
								goods_id: goods_id
							})
							uni.$u.toast('删除成功')
							that.goodsList = that.goodsList.filter(ele => ele.goods_id != goods_id)
							// that.pageInfo.page = 1
							// that.getGoodsById()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			handelChangeStatus() {
				this.status = 'loading'
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 100vh;
		background-color: #f3f4f6;
		box-sizing: border-box;
	}



	.popup_content {
		background-color: #fff;
		margin: 20rpx 10rpx;
		border-radius: 20rpx;

		.top_box {
			display: flex;
			justify-content: space-between;
			font-size: 26rpx;
			padding: 20rpx;

			.btn {
				width: 120rpx;
				height: 70rpx;
				background-color: #39cccc;
				color: #fff;
				text-align: center;
				line-height: 70rpx;
				border-radius: 35rpx;

			}

			.ipt_box {
				display: flex;
				align-items: center;
				width: 480rpx;
				height: 70rpx;
				padding: 0 20rpx;
				background-color: #f2f2f2;
				border-radius: 35rpx;
				box-sizing: border-box;

				input {
					margin-left: 20rpx;
				}
			}

			.text_box {
				color: #60aeff;
				line-height: 70rpx;
			}
		}
	}

	.goods_box {
		display: flex;
		height: 1300rpx;
		// padding-bottom: 20rpx;
		box-sizing: border-box;

		.left_box {
			width: 200rpx;
			// height: 1000rpx;
			background-color: #fff;
			overflow: auto;

			.sort_item {
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
			}

		}

		.right_box {
			flex: 1;
			width: 400rpx;
			// height: 1000rpx;
			overflow: auto;
			background-color: #f8f8f8;
		}

		.goods_item {
			display: flex;
			justify-content: space-between;
			padding: 20rpx;
			border-bottom: 1px solid #c0c0c0;


			.img_box {
				width: 80rpx;
				height: 80rpx;

				image {
					width: 100%;
					height: 80rpx;
				}
			}

			.info {
				margin-left: 20rpx;
				width: 200rpx;

				.goodsName {
					font-size: 30rpx;
					margin-bottom: 10rpx;
				}

				.goodsNum {
					font-size: 26rpx;
					color: #f9b85c;
				}
			}

			.addGoods {
				display: flex;
				align-items: center;

				.right {
					width: 90rpx;
					height: 50rpx;
					background-color: #39cccc;
					color: #fff;
					text-align: center;
					line-height: 50rpx;
					border-radius: 35rpx;
					font-size: 24rpx;
				}
			}
		}
	}

	.sort_item_active {
		background-color: #f8f8f8;
		border-left: 8rpx solid #39cccc;
		color: #39cccc;
	}

	// .minus {
	// 	width: 44rpx;
	// 	height: 44rpx;
	// 	border: 1px solid #f9b85c;
	// 	border-radius: 50%;
	// 	display: flex;
	// 	justify-content: center;
	// 	align-items: center;
	// }

	// .input {
	// 	// padding: 0 10rpx;
	// }

	// .plus {
	// 	width: 44rpx;
	// 	height: 44rpx;
	// 	border: 1px solid #ff0000;
	// 	border-radius: 50%;
	// 	display: flex;
	// 	justify-content: center;
	// 	align-items: center;
	// }

	.empty_box {
		margin: 300rpx 0;
	}

	.hidden_text {
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		/* 设置最大显示行数 */
		-webkit-box-orient: vertical;
		text-overflow: ellipsis;
		word-wrap: break-word;
	}
</style>